import { createRouter, createWebHistory } from 'vue-router'
import Layout from '../layout/Layout.vue'
import Login from '../views/Login.vue'
import Home from '../views/Home.vue'
import Assistant from '../views/Assistant.vue'
import Community from '../views/Community.vue'
import Profile from '../views/Profile.vue'
import CreateTopic from '../views/manage/CreateTopic.vue'
import UserManage from '../views/manage/UserManage.vue'
import AppManage from '../views/manage/AppManage.vue'
import ProjectManage from '../views/manage/ProjectManage.vue'
import ScoreManage from '../views/manage/ScoreManage.vue'
import FeedbackManage from '../views/manage/FeedbackManage.vue'
import Statistics from '../views/manage/Statistics.vue'

const router = createRouter({
  history: createWebHistory(),
  routes: [
    {
      path: '/login',
      name: 'Login',
      component: Login,
      meta: {
        title: '登录'
      }
    },
    {
      path: '/',
      component: Layout,
      children: [
        {
          path: '',
          name: 'Home',
          component: Home,
          meta: { 
            requiresAuth: true,
            title: '首页'
          }
        },
        {
          path: 'create-topic',
          name: 'CreateTopic',
          component: CreateTopic,
          meta: {
            requiresAuth: true,
            title: '创建客题'
          }
        },
        {
          path: 'user-manage',
          name: 'UserManage',
          component: UserManage,
          meta: {
            requiresAuth: true,
            title: '用户管理'
          }
        },
        {
          path: 'app-manage',
          name: 'AppManage',
          component: AppManage,
          meta: {
            requiresAuth: true,
            title: '应用管理'
          }
        },
        {
          path: 'project-manage',
          name: 'ProjectManage',
          component: ProjectManage,
          meta: {
            requiresAuth: true,
            title: '题目管理'
          }
        },
        {
          path: 'score-manage',
          name: 'ScoreManage',
          component: ScoreManage,
          meta: {
            requiresAuth: true,
            title: '评分管理'
          }
        },
        {
          path: 'feedback-manage',
          name: 'FeedbackManage',
          component: FeedbackManage,
          meta: {
            requiresAuth: true,
            title: '回答管理'
          }
        },
        {
          path: 'statistics',
          name: 'Statistics',
          component: Statistics,
          meta: {
            requiresAuth: true,
            title: '应用统计'
          }
        },
        {
          path: 'assistant',
          name: 'Assistant',
          component: Assistant,
          meta: { 
            requiresAuth: true,
            title: 'AI助手'
          }
        },
        {
          path: 'community',
          name: 'Community',
          component: Community,
          meta: { 
            requiresAuth: true,
            title: '学习社区'
          }
        },
        {
          path: 'profile',
          name: 'Profile',
          component: Profile,
          meta: { 
            requiresAuth: true,
            title: '个人中心'
          }
        }
      ]
    }
  ]
})

// 全局路由守卫
router.beforeEach((to, from, next) => {
  const token = localStorage.getItem('token')
  
  // 如果访问登录页且已登录,跳转到首页
  if (to.path === '/login' && token) {
    next('/')
    return
  }
  
  // 如果页面需要认证且未登录,跳转到登录页
  if (to.meta.requiresAuth && !token) {
    next('/login')
    return
  }
  
  next()
})

export default router 